package net.wanho.task;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import net.wanho.mapper.ProductMapper;
import net.wanho.po.Product;
import net.wanho.vo.MessageVo;
import net.wanho.websocket.WebSocketServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Author：汤小洋
 * Date：2024-10-24 17:03
 * Description：<描述>
 */
@Component
public class WarningTask {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private WebSocketServer webSocketServer;

    /**
     * 当库存数量小于0时，通过消息推送和短信通知进行提醒！（每小时检测一次）
     */
    @Scheduled(cron = "0 0 * * * ?")
    // @Scheduled(cron = "0/10 * * * * ?") // 测试
    public void checkProductInvent() {
        List<Product> list = productMapper.selectInventDanger();

        String content = "以下商品库存不足：" + list.stream().map(item -> item.getProductName()).collect(Collectors.joining("、"));
        MessageVo messageVo = new MessageVo(1, content);

        // 发送消息
        webSocketServer.sendToAllClient(JSONUtil.toJsonStr(messageVo));
    }

}
